using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Autorun
{
	public partial class PaneLog : Autorun.PaneBase
	{
		private const string ICO_INFO = "Info";
		private const string ICO_WARNING = "Warning";
		private const string ICO_ERROR = "Error";

		#region "Constructors and overriden methods"
		public PaneLog()
		{
			InitializeComponent();
			LoadIcons();
		}

		public override bool Initialize(IPresenter sender, ShowPaneEventArgs e)
		{
			_parent = sender;

			if ((e != null) && (e.Args != null) && (e.Args.Length != 0))
				if (e.Args[0] is ServerManagement)
					_proxy = (ServerManagement)e.Args[0];
				else
					_proxy = null;
			else
				_proxy = null;

			RefreshInformation();

			return true;
		}

		#endregion

		#region "Procedures"
		private void LoadIcons()
		{
			mnuRefresh.Image = Properties.Resources.icoRefresh.ToBitmap();

			myImages.Images.Clear();
			myImages.Images.Add(ICO_INFO, Properties.Resources.Symbol_Information);
			myImages.Images.Add(ICO_WARNING, Properties.Resources.Symbol_Exclamation);
			myImages.Images.Add(ICO_ERROR, Properties.Resources.Symbol_Error);
		}

		private void RefreshInformation()
		{
			ListViewItem lvi;
			DataTable items;
			ListViewItem.ListViewSubItem lvsi;
			string imgKey;

			Cursor = Cursors.WaitCursor;

			myList.Items.Clear();
			if (_proxy != null)
			{
				Caption = string.Format("{0}: Event log", _proxy.Information.MachineName);

				items = _proxy.Log.LogEntries;
				foreach (DataRow item in items.Rows)
				{
					switch ((byte)item["logSeverity"])
					{
						case 0:
							imgKey = null;
							break;
						case 1:
							imgKey = ICO_INFO;
							break;
						case 2:
							imgKey = ICO_WARNING;
							break;
						case 4:
							imgKey = ICO_ERROR;
							break;
						default:
							imgKey = null;
							break;
					}

					lvi = myList.Items.Add(item["logDateTime"].ToString(), imgKey);
					lvi.Tag = item["logId"];
					lvi.SubItems.Add(item["tskCaption"].ToString());
					lvi.SubItems.Add(item["fkschId"].ToString());
					lvi.SubItems.Add(item["logKey"].ToString());
					lvsi = lvi.SubItems.Add(item["logSeverityCaption"].ToString());
					lvsi.Tag = int.Parse(item["logSeverity"].ToString());
					lvi.SubItems.Add(item["logMessage"].ToString());
					lvi.SubItems.Add(item["logData"].ToString());
					switch ((int)lvsi.Tag)
					{
						case 0:
							lvi.Group = myList.Groups["grpVerbose"];
							break;
						case 1:
							lvi.Group = myList.Groups["grpInformation"];
							break;
						case 2:
							lvi.Group = myList.Groups["grpWarning"];
							break;
						case 4:
							lvi.Group = myList.Groups["grpError"];
							break;
						default:
							lvi.Group = null;
							break;
					}
				}

				myList.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
				myList.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
				myList.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
				myList.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
				myList.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
				myList.Columns[5].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
				myList.Columns[6].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
			}
			else
				Caption = "Not connected";

			Cursor = Cursors.Default;
		}
		#endregion

		#region "Controls Event Handlers"
		private void myList_SelectedIndexChanged(object sender, EventArgs e)
		{
			Color colorON;
			Color colorOFF;
			string contextKey;

			// ===============================================================
			// === Pre-Process                                             ===
			// ===============================================================
			colorON = Color.Thistle;
			colorOFF = myList.BackColor;
			if ((myList.SelectedItems.Count == 0) || (myList.SelectedItems[0].SubItems[3].Text == ""))
				contextKey = "zzz";
			else
				contextKey = myList.SelectedItems[0].SubItems[3].Text;

			// ===============================================================
			// === Process                                                 ===
			// ===============================================================
			foreach (ListViewItem item in myList.Items)
				if (item.SubItems[3].Text == contextKey)
					item.BackColor = colorON;
				else
					item.BackColor = colorOFF;
		}

		private void mnuRefresh_Click(object sender, EventArgs e)
		{
			RefreshInformation();
		}

		private void mnuClearAll_Click(object sender, EventArgs e)
		{

			if ((_proxy != null) && (MessageBox.Show("Do you really want to clear the log?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
			{
				_proxy.Log.ClearLog();
				RefreshInformation();
			}
		}

		private void mnuGroup_Click(object sender, EventArgs e)
		{
			myList.ShowGroups = true;
			RefreshInformation();
			mnuGroup.Enabled = false;
			mnuUngroup.Enabled = true;
		}

		private void mnuUngroup_Click(object sender, EventArgs e)
		{
			myList.ShowGroups = false;
			RefreshInformation();
			mnuGroup.Enabled = true;
			mnuUngroup.Enabled = true;
		}

		private void myList_DoubleClick(object sender, EventArgs e)
		{
			LogDialog dlg;

			if (myList.SelectedItems.Count != 0)
			{
				dlg = new LogDialog();
				dlg.SetData(_proxy, (int)myList.SelectedItems[0].Tag);
				dlg.ShowDialog();
			}
		}

		private void mnuDetail_Click(object sender, EventArgs e)
		{
			myList_DoubleClick(sender, e);
		}
		#endregion

		private void mnuClearLevel_Click(object sender, EventArgs e)
		{
			SeverityLevels level;

			if (_proxy != null)
			{
				level = (SeverityLevels)(int.Parse(((ToolStripMenuItem)sender).Tag.ToString()));
				_proxy.Log.ClearLog(level);
				RefreshInformation();
			}
		}
	}
}

